/*
 * Copyright © 2012 by PropSage Pte Ltd. 
 * All Rights Reserved.
 * licensing@propsage.com
 * http://www.propsage.com/terms.html
 * 
 * 
 * Javascript for /case/index
 * 
 * @author Poh Nean
 */
propsage.namespace("modules.admin.users");

modules.admin.users.promote = (function($, ui){
    
	function initComponents()
	{
		
	}
	
	function initEventHandlers()
	{
		$('#Promotion_from_date_datepicker').change(onEffectDateChange);
		$('input:submit[value="Save"]').click(onSubmitClick);
	}
	
	function onEffectDateChange()
	{
		var fromDate = $('#Promotion_from_date').val();
		ui.doAjax('POST', baseUrl + '/admin/users/ajaxLoadPrevailingDesignations', {
			date: fromDate
		}, onActiveDesignationsLoadSuccess);
	}
	
	function onActiveDesignationsLoadSuccess(response)
	{
		var listOptions = response.designations;
		var pos = response.pos;
		
		$('#effectiveDesignationSelect option:gt(0)').remove();
		
		$.each(pos, function(i,val){
			$('#effectiveDesignationSelect').append('<option value="'+ val +'">' + listOptions[val] + '</option>');
		});
	}
	
	function onSubmitClick()
	{
		var date = $('#Promotion_from_date').val();
		var designationId = $('#effectiveDesignationSelect').val();
		
		if(!date){
			alert('Please select effective date');
			$('#Promotion_from_date_datepicker').focus();
			return false;
		}
		if(!designationId){
			alert('Please select designation');
			$('#effectiveDesignationSelect').focus();
			return false;
		}
	}
	
	$(document).ready(function(){
		initComponents();
		initEventHandlers();
	});
    
}(jQuery, propsage.ui));
